#region Copyright (c) 2006-2012 nHydrate.org, All Rights Reserved
// --------------------------------------------------------------------- *
//                           NHYDRATE.ORG                                *
//              Copyright (c) 2006-2012 All Rights reserved              *
//                                                                       *
//                                                                       *
// This file and its contents are protected by United States and         *
// International copyright laws.  Unauthorized reproduction and/or       *
// distribution of all or any portion of the code contained herein       *
// is strictly prohibited and will result in severe civil and criminal   *
// penalties.  Any violations of this copyright will be prosecuted       *
// to the fullest extent possible under law.                             *
//                                                                       *
// THE SOURCE CODE CONTAINED HEREIN AND IN RELATED FILES IS PROVIDED     *
// TO THE REGISTERED DEVELOPER FOR THE PURPOSES OF EDUCATION AND         *
// TROUBLESHOOTING. UNDER NO CIRCUMSTANCES MAY ANY PORTION OF THE SOURCE *
// CODE BE DISTRIBUTED, DISCLOSED OR OTHERWISE MADE AVAILABLE TO ANY     *
// THIRD PARTY WITHOUT THE EXPRESS WRITTEN CONSENT OF THE NHYDRATE GROUP *
//                                                                       *
// UNDER NO CIRCUMSTANCES MAY THE SOURCE CODE BE USED IN WHOLE OR IN     *
// PART, AS THE BASIS FOR CREATING A PRODUCT THAT PROVIDES THE SAME, OR  *
// SUBSTANTIALLY THE SAME, FUNCTIONALITY AS THIS PRODUCT                 *
//                                                                       *
// THE REGISTERED DEVELOPER ACKNOWLEDGES THAT THIS SOURCE CODE           *
// CONTAINS VALUABLE AND PROPRIETARY TRADE SECRETS OF NHYDRATE,          *
// THE REGISTERED DEVELOPER AGREES TO EXPEND EVERY EFFORT TO             *
// INSURE ITS CONFIDENTIALITY.                                           *
//                                                                       *
// THE END USER LICENSE AGREEMENT (EULA) ACCOMPANYING THE PRODUCT        *
// PERMITS THE REGISTERED DEVELOPER TO REDISTRIBUTE THE PRODUCT IN       *
// EXECUTABLE FORM ONLY IN SUPPORT OF APPLICATIONS WRITTEN USING         *
// THE PRODUCT.  IT DOES NOT PROVIDE ANY RIGHTS REGARDING THE            *
// SOURCE CODE CONTAINED HEREIN.                                         *
//                                                                       *
// THIS COPYRIGHT NOTICE MAY NOT BE REMOVED FROM THIS FILE.              *
// --------------------------------------------------------------------- *
#endregion
using System.Diagnostics;
using nHydrate.Generator.Common.Logging;

namespace nHydrate.Generator.Common.Util
{
	/// <summary>
	/// Summary description for PerformanceCounterSingleton.
	/// </summary>
	public class PerformanceCounterSingleton
	{

		private static readonly PerformanceCounterSingleton _instance = null;

		private PerformanceCounter _TotalAppointments;
		private PerformanceCounter _AppointmentsPerSecond;

		static PerformanceCounterSingleton()
		{
			_instance = new PerformanceCounterSingleton();
			_instance.InstallPerformanceCounters();
			_instance.InitializePerformanceCounters();
		}

		public static void Intialize()
		{
			nHydrateLog.LogInfo("Initialize Performance Counters");
		}

		private void InstallPerformanceCounters()
		{
			if (!PerformanceCounterCategory.Exists("nHydrate"))
			{
				var counters = new CounterCreationDataCollection();

				// 1. counter for counting totals: PerformanceCounterType.NumberOfItems32
				var totalAppointments = new CounterCreationData();
				totalAppointments.CounterName = "# appointments processed";
				totalAppointments.CounterHelp = "Total number of appointments processed.";
				totalAppointments.CounterType = PerformanceCounterType.NumberOfItems32;
				counters.Add(totalAppointments);

				// 2. counter for counting operations per second:
				//        PerformanceCounterType.RateOfCountsPerSecond32
				var appointmentsPerSecond = new CounterCreationData();
				appointmentsPerSecond.CounterName = "# appointments / sec";
				appointmentsPerSecond.CounterHelp = "Number of operations executed per second";
				appointmentsPerSecond.CounterType = PerformanceCounterType.RateOfCountsPerSecond32;
				counters.Add(appointmentsPerSecond);

				// create new category with the counters above
				PerformanceCounterCategory.Create("nHydrate", "nHydrate Category", counters);
			}

		}

		private void InitializePerformanceCounters()
		{
			_TotalAppointments = new PerformanceCounter();
			_TotalAppointments.CategoryName = "nHydrate";
			_TotalAppointments.CounterName = "# appointments processed";
			_TotalAppointments.MachineName = ".";
			_TotalAppointments.ReadOnly = false;

			_AppointmentsPerSecond = new PerformanceCounter();
			_AppointmentsPerSecond.CategoryName = "nHydrate";
			_AppointmentsPerSecond.CounterName = "# appointments / sec";
			_AppointmentsPerSecond.MachineName = ".";
			_AppointmentsPerSecond.ReadOnly = false;
		}

		public PerformanceCounter TotalAppointments
		{
			get
			{
				return _TotalAppointments;
			}
		}

		public PerformanceCounter AppointmentsPerSecond
		{
			get
			{
				return _AppointmentsPerSecond;
			}
		}


		public static PerformanceCounterSingleton Instance
		{
			get
			{
				return _instance;
			}
		}
	}
}
